package com.bit.frige.sub.food;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.StringTokenizer;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.bit.frige.MainActivity;
import com.bit.frige.R;
import com.bit.frige.DB.DBContactHelper;
import com.bit.frige.DB.FoodDTO;
import com.bit.frige.DB.MyAdapter2;

public class SubFoodContensActivity extends Activity {
	private Button btn,btn1,btn4;
	private ListView mList;
	private int place;
	private ArrayList<FoodDTO> foodlist;
	private DBContactHelper db;
	private MyAdapter2 adapter;
	private int num;
	
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_sub_food_contens);
//		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		Intent intent = getIntent();
		place = intent.getIntExtra("place", 0); //냉동,냉장정보
		
		db = new DBContactHelper(this);
		foodlist = db.getAllUserInfo(place);
		
		listSet();
		mList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View view,int position, long id) {
				FoodDTO food =foodlist.get(position);
				Log.d("롱클릭이벤","아이디값 : "+food.getId());
				num=food.getId();
				DialogSimple(food.getFoodname());
				return false;
			}
		});
		mList.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,
					long arg3) {
				Intent intent = new Intent(getApplicationContext(),SubFoodEditFormActivity.class);
				intent.putExtra("place", place);
				intent.putExtra("id", foodlist.get(position).getId());
				startActivity(intent);
				finish();
				
			}
			
			
		});

	
		
		
		btn = (Button) findViewById(R.id.categobtn);
		btn.setOnClickListener(new OnClickListener() {
		//식품등록 버튼 이벤트
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(getApplicationContext(),SubFoodCategoriActivity.class);
				intent.putExtra("place", place);
				startActivity(intent);
			}
		});
		
//		btn1 = (Button) findViewById(R.id.button2);
//		btn1.setOnClickListener(new OnClickListener() {
//			//홈이벤트	
//			public void onClick(View v) {
//				Intent intentHome = new Intent(getApplicationContext(),MainActivity.class);
//				intentHome.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
//				intentHome.putExtra("load", 1);
//				startActivity(intentHome);
//		}
//		});
	}


	
	
	public void listSet(){
		
		mList = (ListView) findViewById(R.id.foodlist);
		adapter = new MyAdapter2(this, foodlist);
		mList.setAdapter(adapter);
		FoodDTO food;
		for (int i=0; i<foodlist.size(); i++) { 
			food= foodlist.get(i);
			String date = food.getSelfdate().trim();
			Log.d("fooddate","날짜  "+date);
			StringTokenizer stz = new StringTokenizer(date,"-");
			int []day = new int[3];
			for( int k=0; k<3; k++){
				day[k]= Integer.parseInt(stz.nextToken());
			}
			Calendar date1 = Calendar.getInstance();
	        Calendar date2 = Calendar.getInstance();
	        int year = day[0];
	        int month= day[1];
	        int d = day[2];
	        date2.set(year,month-1,d); 
	        long difference = (date2 .getTimeInMillis()-date1.getTimeInMillis())/1000;
	        int dday =(int) (difference/(24*60*60));
	        food.setSelflife(dday);
	        
		}
	}
	void DialogSimple(String foodname){
	    AlertDialog.Builder alt_bld = new AlertDialog.Builder(SubFoodContensActivity.this);
	    alt_bld.setMessage("[ "+foodname+" ] 삭제하시겠습니까? ").setCancelable(
	        false).setPositiveButton("예",
	        new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int id) {
	            // 예 버튼
	        	db.deleteUserInfo(num);
	        	foodlist = db.getAllUserInfo(place);
				listSet();
				adapter.notifyDataSetChanged();
				
	        }
	        }).setNegativeButton("아니오",
	        new DialogInterface.OnClickListener() {
	        public void onClick(DialogInterface dialog, int id) {
	            // 아니오 버튼
	            dialog.cancel();
	        }
	        });
	    AlertDialog alert = alt_bld.create();
	    alert.setTitle("삭제 확인");
	    alert.setIcon(R.drawable.smalicon);
	    alert.show();
	}
	
	
	
	
}
